package by.dezhits.makeanote.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import by.dezhits.makeanote.R;
import by.dezhits.makeanote.adapter.FoldersCursorAdapter;
import by.dezhits.makeanote.content.contract.FoldersContract;
import by.dezhits.makeanote.content.contract.NotesContract;

@SuppressLint("ValidFragment")
public class FoldersFragment extends DialogFragment implements
		OnItemClickListener, OnClickListener {

	private FoldersCursorAdapter foldersCursorAdapter;
	private ListView mListView;
	private FoldersCallback mCallback;
	private View mHeaderView;
	private View mFooterView;

	private Integer id;
	private String name;
	private String pass;

	private View mNewFolderDialogView;
	private ImageButton mNewFolderDialogButton;
	private EditText mNewFolderDialogEditText;
	private AlertDialog mNewFolderAlertDialog;

	private View mCheckPassDialogView;
	private ImageButton mCheckPassDialogButton;
	private EditText mCheckPassDialogEditText;
	private AlertDialog mCheckPassAlertDialog;

	private static final int HEADER_VIEW = 0;
	private static final int FOOTER_VIEW = 1;
	private static final int NEW_FOLDER = 2;
	private static final int CHECK_PASS = 3;

	private static final String ALL_NOTES = NotesContract.NotesColumns.CLOSED
			+ "='FALSE'";

	public interface FoldersCallback {
		void onFolderChosen(String selection, Integer id, String folder,
				boolean closed);
	}

	public FoldersFragment(FoldersCallback c) {
		mCallback = c;
	}

	private static final int FOLDERS_LOADER = 0;

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setStyle(STYLE_NO_TITLE, 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_folders, null);
		mListView = (ListView) view.findViewById(R.id.list_view);
		foldersCursorAdapter = new FoldersCursorAdapter(this.getActivity(),
				null);
		mHeaderView = inflater.inflate(R.layout.view_folders_headerview, null);
		mFooterView = inflater.inflate(R.layout.view_folders_footerview, null);

		mNewFolderDialogView = inflater.inflate(
				R.layout.view_dialog_new_folder, null);
		mNewFolderDialogButton = (ImageButton) mNewFolderDialogView
				.findViewById(R.id.btn_dialog_new_folder);
		mNewFolderDialogEditText = (EditText) mNewFolderDialogView
				.findViewById(R.id.et_dialog_new_folder);
		mNewFolderDialogButton.setId(NEW_FOLDER);

		mCheckPassDialogView = inflater.inflate(
				R.layout.view_dialog_check_pass, null);
		mCheckPassDialogButton = (ImageButton) mCheckPassDialogView
				.findViewById(R.id.btn_dialog_check_pass);
		mCheckPassDialogEditText = (EditText) mCheckPassDialogView
				.findViewById(R.id.et_dialog_check_pass);
		mCheckPassDialogButton.setId(CHECK_PASS);

		mHeaderView.setId(HEADER_VIEW);
		mFooterView.setId(FOOTER_VIEW);
		mListView.addHeaderView(mHeaderView);
		mListView.addFooterView(mFooterView);
		mNewFolderDialogButton.setOnClickListener(this);
		mCheckPassDialogButton.setOnClickListener(this);
		mHeaderView.setOnClickListener(this);
		mFooterView.setOnClickListener(this);

		AlertDialog.Builder adb = new AlertDialog.Builder(this.getActivity());
		adb.setView(mNewFolderDialogView).setTitle(R.string.new_folder);
		mNewFolderAlertDialog = adb.create();
		adb.setView(mCheckPassDialogView).setTitle(R.string.enter_password);
		mCheckPassAlertDialog = adb.create();
		mListView.setAdapter(foldersCursorAdapter);
		mListView.setOnItemClickListener(this);
		resetList(null);
		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View clickedView,
			int position, long itemId) {
		Cursor c = (Cursor) mListView.getItemAtPosition(position);
		name = c.getString(c
				.getColumnIndex(FoldersContract.FoldersColumns.NAME));
		pass = c.getString(c
				.getColumnIndex(FoldersContract.FoldersColumns.PASS));
		id = c.getInt(c.getColumnIndex(FoldersContract.FoldersColumns.ID));
		if (pass == null) {
			mCallback.onFolderChosen(NotesContract.NotesColumns.FOLDER + "='"
					+ name + "'", id, name, false);
			dismiss();
		} else {
			mCheckPassAlertDialog.show();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case HEADER_VIEW:
			mCallback.onFolderChosen(ALL_NOTES, null, null, false);
			dismiss();
			break;
		case FOOTER_VIEW:
			mNewFolderAlertDialog.show();
			break;
		case CHECK_PASS:
			if (mCheckPassDialogEditText.getText().toString().equals(pass)) {
				mCallback.onFolderChosen(NotesContract.NotesColumns.FOLDER
						+ "='" + name + "'", id, name, true);
				mCheckPassAlertDialog.dismiss();
				dismiss();
				break;
			} else {
				Toast.makeText(getActivity(), R.string.wrong_password,
						Toast.LENGTH_LONG).show();
			}
		case NEW_FOLDER:
			String newFolder;
			if (!TextUtils.isEmpty((newFolder = mNewFolderDialogEditText
					.getText().toString()))) {
				ContentValues values = new ContentValues();
				values.put(getString(R.string.name), newFolder);
				Uri uri = getActivity().getContentResolver().insert(
						FoldersContract.FoldersColumns.CONTENT_URI, values);
				long id = Long.parseLong(uri.getLastPathSegment());
				if (id == -1) {
					Toast.makeText(getActivity(),
							R.string.this_name_is_already_in_use, Toast.LENGTH_LONG)
							.show();
				} else {
					mNewFolderDialogEditText.setText(null);
					mNewFolderAlertDialog.dismiss();
				}
			}

		}

	}

	private void resetList(final String selection) {
		getLoaderManager().restartLoader(FOLDERS_LOADER, null,
				new LoaderCallbacks<Cursor>() {

					@Override
					public Loader<Cursor> onCreateLoader(int paramInt,
							Bundle paramBundle) {
						return new CursorLoader(FoldersFragment.this
								.getActivity(),
								FoldersContract.FoldersColumns.CONTENT_URI,
								null, selection, null,
								FoldersContract.FoldersColumns.ID);
					}

					@Override
					public void onLoadFinished(Loader<Cursor> paramLoader,
							Cursor paramD) {
						foldersCursorAdapter.swapCursor(paramD);
					}

					@Override
					public void onLoaderReset(Loader<Cursor> paramLoader) {
					}
				});
	}

}
